package com.whty.module.controller;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.whty.common.annotation.Log;
import com.whty.common.base.AjaxResult;
import com.whty.common.enums.BusinessType;
import com.whty.framework.web.page.TableDataInfo;
import com.whty.module.service.IDepotHeadService;
import com.whty.system.domain.DepotHeadItem;
import com.whty.system.service.IDepotHeadItemService;
import com.whty.web.core.base.BaseController;

/**
 * 入库明细 信息操作处理
 * 
 * @author mario
 * @date 2018-10-31
 */
@Controller
@RequestMapping("/module/depotHeadItem")
public class DepotHeadItemController extends BaseController
{
    private String prefix = "module/depotHeadItem";
	
	@Autowired
	private IDepotHeadItemService depotHeadItemService;
	@Autowired
	private IDepotHeadService depotHeadService;
	
	@RequiresPermissions("module:depotHeadItem:view")
	@GetMapping()
	public String depotHeadItem()
	{
	    return prefix + "/depotHeadItem";
	}
	
	/**
	 * 查询入库明细列表
	 */
	@RequiresPermissions("module:depotHeadItem:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(DepotHeadItem depotHeadItem)
	{
		startPage();
        List<DepotHeadItem> list = depotHeadItemService.selectDepotHeadItemList(depotHeadItem);
		return getDataTable(list);
	}
	
	/**
	 * 新增入库明细
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存入库明细
	 */
	@RequiresPermissions("module:depotHeadItem:add")
	@Log(title = "入库明细", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(DepotHeadItem depotHeadItem)
	{		
		return toAjax(depotHeadItemService.insertDepotHeadItem(depotHeadItem));
	}

	/**
	 * 修改入库明细
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Integer id, ModelMap mmap)
	{
		DepotHeadItem depotHeadItem = depotHeadItemService.selectDepotHeadItemById(id);
		mmap.put("depotHeadItem", depotHeadItem);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存入库明细
	 */
	@RequiresPermissions("module:depotHeadItem:edit")
	@Log(title = "入库明细", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(DepotHeadItem depotHeadItem)
	{		
		return toAjax(depotHeadItemService.updateDepotHeadItem(depotHeadItem));
	}
	
	/**
	 * 删除入库明细
	 */
	@RequiresPermissions("module:depotHeadItem:remove")
	@Log(title = "入库明细", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(depotHeadItemService.deleteDepotHeadItemByIds(ids));
	}
	
	/**
	 * 获取某个入库批次下当前可用(未分配)的起始序列号
	 * @param depotHeadNo
	 * @return
	 */
	@PostMapping("/getBeginSerialNumber")
	@ResponseBody
	public List<String> getBeginSerialNumber(String depotHeadNo){
		List<String> snList = new ArrayList<String>();
		if(StringUtils.isNotBlank(depotHeadNo)){
			DepotHeadItem depotHeadItem = new DepotHeadItem();
			depotHeadItem.setDepotHeadNo(depotHeadNo);
			depotHeadItem.setAllocationStatus("00");
			List<DepotHeadItem> depotHeadItemList = depotHeadItemService.selectDepotHeadItemList(depotHeadItem);
			if(CollectionUtils.isNotEmpty(depotHeadItemList)){
				for (DepotHeadItem depotHeadItem2 : depotHeadItemList) {
					snList.add(depotHeadItem2.getLabelSerialNumber());
				}
			}
			if(CollectionUtils.isNotEmpty(snList)){
				Collections.sort(snList);
			}
		}
		return snList;
	}
	
	/**
	 * 查询同一入库批次下是否已经发行的标签
	 * @param depotHeadNo
	 * @return
	 */
	@PostMapping("/getReleaseStatus")
	@ResponseBody
	public Boolean getReleaseStatus(String depotHeadNo){
		boolean flag = false;
		DepotHeadItem depotHeadItem = new DepotHeadItem();
		depotHeadItem.setDepotHeadNo(depotHeadNo);
		List<DepotHeadItem> depotHeadItemList = depotHeadItemService.selectDepotHeadItemList(depotHeadItem);
		if(CollectionUtils.isNotEmpty(depotHeadItemList)){
			for (DepotHeadItem depotHeadItem2 : depotHeadItemList) {
				String releaseStatus = depotHeadItem2.getReleaseStatus();
				if(releaseStatus.equals("02")){ //已发行的状态
					flag = true;
					break;
				}
			}
		}
		return flag;
	}
}
